package com.iLoo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iLoo.entity.Emp;
import com.iLoo.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@CrossOrigin
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 将修改页面的数据传入到数据库
     * @param emp
     * @param photo
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping("updateInfo")
    public String updateInfo(Emp emp,@RequestParam("photoName") MultipartFile photo,Model model) throws IOException {
        String newFile;
        if(!photo.getOriginalFilename().isEmpty()) {
            newFile = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
            photo.transferTo(new File("H:\\学习\\研究生\\emp_vue_bilibili\\src\\main\\resources\\static\\img", newFile));

        }else {
            Emp emp_tmp = empService.getById(emp.getId());
            newFile=emp_tmp.getPath();
        }
        emp.setPath(newFile);
        empService.updateById(emp);
        return "forward:/findAll";
    }

    /**
     * 获取要修改的员工信息，然后跳转到修改界面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("updateEmp")
    public String toUpdataEmp(@RequestParam(value="id",defaultValue = "1")Integer id,Model model){
        Emp emp = empService.getById(id);
        model.addAttribute("empinfo",emp);
        System.out.println("test");
        return "updateEmp";
    }


    /**
     * 删除
     * @param id
     * @param photo
     * @return
     */
    @RequestMapping("deleteEmp")
    public String delete(@RequestParam(value="id",defaultValue = "1")Integer id,@RequestParam(value="photo")String photo){
        empService.removeById(id);
        File file=new File("H:\\学习\\研究生\\emp_vue_bilibili\\src\\main\\resources\\static\\img",photo);
        if(file.exists()){
            file.delete();
        }
        return "forward:/findAll";
    }


    /**
     * 获取员工列表的方法
     * @param model
     * @return
     */
    @RequestMapping("findAll")
    public String findAll(@RequestParam(value="pn",defaultValue = "1")Integer pn, Model model){
        //就不用自己写的方法了
        //试一下MyBatis-plus的
        //List<Emp> allemp = empService.findAll();
        List<Emp> allemp = empService.list();
        model.addAttribute("allemp",allemp);

        Page<Emp> empPage = new Page<>(pn,2);
        Page<Emp> page =empService.page(empPage,null);
        if(pn<1){
            pn=1;
        }
        if(pn>page.getPages()){
            pn=(int)page.getPages();
        }
        empPage = new Page<>(pn,2);
        page =empService.page(empPage,null);

        model.addAttribute("nowpage",pn);
        model.addAttribute("page",page);

        return "emplist";
    }


    @RequestMapping("empAdd")
    public String empAdd(Emp emp,@RequestParam("photo") MultipartFile photo,Model model) throws IOException {


        String newFile= UUID.randomUUID().toString()+"."+ FilenameUtils.getExtension(photo.getOriginalFilename());

        photo.transferTo(new File("H:\\学习\\研究生\\emp_vue_bilibili\\src\\main\\resources\\static\\img",newFile));
        emp.setPath(newFile);

        empService.save(emp);
        return "forward:/findAll";
    }


    @RequestMapping("addEmp")
    public String success()  {

        return "addEmp";
    }
}
